import Taro from '@tarojs/taro'
import getBaseUrl from './baseUrl'
import interceptors from './interceptors'

interceptors.forEach(interceptorItem => Taro.addInterceptor(interceptorItem))
function getParams(params) {
  let str = ''
  console.error('getParams(data.params)',params)
  if (params) {
    const temp = Object.entries(params)
    // console.error(temp, 'temp')
    for (const i in temp) {
      if (i == temp.length - 1) {
        str += temp[i][0] + '=' + temp[i][1]
      } else {
        str += temp[i][0] + '=' + temp[i][1] + '&'
      }
    }
  }
  return str
}
class httpRequest {

  baseOptions(params, method = "GET") {
    const { url, data } = params;
    const BASE_URL = getBaseUrl(url);
    // console.log(BASE_URL, 'BASE_URL')

    let contentType = "application/json";
    contentType = params.contentType || contentType;
    let requestUrl = '';
    // console.error(getParams(data.params), 'aaaa')
    if (method == 'GET') {
      const paramsData = getParams(data.params).split('=');
      console.log(paramsData,'paramsData')
      for (let i = 0; i < paramsData.length; i++) {
        // if (BASE_URL.indexOf(paramsData[i][1]) > 0) {
        //   requestUrl = BASE_URL;
        //   continue;
        // } else {
          requestUrl = BASE_URL + '?' + getParams(data.params)
        // }
      }
      const option = {
        url: requestUrl,
        method: method,
        header: {
          'content-type': contentType,
          tenantId: 'lovelorn',
          devType: 2,
          // userId: Taro.getStorageSync('loginVO') ? Taro.getStorageSync('loginVO').user.userId : '',
          // token: Taro.getStorageSync('loginVO') ? Taro.getStorageSync('loginVO').authInfo.token : '',
          token: 'O6bpijaAh5CM1591270669453',
          userid: '585227567718400'
        }
      };
      return Taro.request(option);
    } else {
      const option = {
        url: BASE_URL,
        data: data,
        method: method,
        header: {
          'content-type': contentType,
          tenantId: 'lovelorn',
          devType: 4,
          userId: Taro.getStorageSync('loginVO') ? Taro.getStorageSync('loginVO').user.userId : '',
          token: Taro.getStorageSync('loginVO') ? Taro.getStorageSync('loginVO').authInfo.token : '',
        }
      };
      return Taro.request(option);
    }


  }

  get(url, data = "") {
    const option = { url, data };
    return this.baseOptions(option, 'GET');
  }
  // export function httpGet<T>(url: string, config?: HttpConfig): AxiosPromise<T> {
  //   return httpInit.get(url, config)
  // }


  post(url, data, contentType) {
    const params = { url, data, contentType };
    return this.baseOptions(params, "POST");
  }

  put(url, data = "") {
    const option = { url, data };
    return this.baseOptions(option, "PUT");
  }

  delete(url, data = "") {
    const option = { url, data };
    return this.baseOptions(option, "DELETE");
  }

}

// export default new httpRequest()
export function httpPost(url, data, contentType) {
  return new httpRequest().post(url, data, contentType)
}
export function httpGet(url, data = "") {
  return new httpRequest().get(url, data)
}
export function httpDelete(url, data = "") {
  return new httpRequest().delete(url, data)
}
export function httpPut(url, data = "") {
  return new httpRequest().put(url, data)
}
